// test.h
//
// Unit Test for resource manager

#include "unitTest.h"
#include "resourceMgr.h"
#include "xmlTools.h"

using namespace simple;

namespace simple_test{
    UTFUNC(stringresource)
    {
        setDescription ("StringResource test");

        StringResource r1 (L"This is string 1", L"Notes");
        StringResource r2 (L"This is string 2", L"Notes 2");
        ResourceMgr::gOnly().exportStrings ("test.xml");

        VERIFY (r1.string() == L"This is string 1");
        VERIFY (r2.string() == L"This is string 2");

        // Clear our list and make sure it's empty
        ResourceMgr::gOnly().reset ();
        VERIFY (r1.string() == L"");
        VERIFY (r2.string() == L"");

        // Import the list and make sure it is back
        ResourceMgr::gOnly().importStrings ("test.xml");
        VERIFY (r1.string() == L"This is string 1");
        VERIFY (r2.string() == L"This is string 2");

        // Test conversion operator, operator()
        std::wstring s1 = r1;
        std::wstring s2 = r2;
        VERIFY (s1 == L"This is string 1");
        VERIFY (s2 == L"This is string 2");

    }

    UTFUNC(xml1)
    {
        setDescription ("xml test");
        std::wstring xml1 = L"<phrase><id>1</id><string>Hello World</string></phrase>";

        // Test parseNextTag()
        XMLTools xmlTool1 (xml1, WideTools::gOnly().endian());
        std::wstring tag = xmlTool1.parseNextTag ();
        VERIFY (tag == L"<phrase>");
        tag = xmlTool1.parseNextTag ();
        VERIFY (tag == L"<id>");
        tag = xmlTool1.parseNextTag ();
        VERIFY (tag == L"</id>");
        tag = xmlTool1.parseNextTag ();
        VERIFY (tag == L"<string>");
        tag = xmlTool1.parseNextTag ();
        VERIFY (tag == L"</string>");
        tag = xmlTool1.parseNextTag ();
        VERIFY (tag == L"</phrase>");
        tag = xmlTool1.parseNextTag ();
        VERIFY (tag == L"");

        // Test parse()
        XMLTools xmlTool2 (xml1, WideTools::gOnly().endian());
        XMLElement element = xmlTool2.parse ();
        VERIFY (element.name == L"<phrase>");
        VERIFY (element.value == L"<id>1</id><string>Hello World</string>");
        
        element = xmlTool2.parse ();
        VERIFY (element.name == L"");
        VERIFY (element.value == L"");

        // Test parseTag () (used directly by the constructor)
        std::wstring xml2 = L"<?xml version=\"1.0\" encoding=\"UTF-16\" ?><!-- Comment --><resources><phrase><id>1</id><string>Hello World</string></phrase><!-- Another Comment --></resources>";
        XMLTools xmlTool3 (xml2, WideTools::gOnly().endian());
        VERIFY (xmlTool3.xml() == L"<resources><phrase><id>1</id><string>Hello World</string></phrase></resources>");
    }

    UTFUNC(write)
    {
        setDescription ("write test");

        XMLTools xml;
        xml.writeResource (101, L"String", L"Notes");
        xml.writeResource (202, L"String 2", L"Notes 2");
        xml.writeResource (303, L"String 3", L"");
        xml.writeTrailing ();

        std::wstring expected;
        expected.push_back (WideTools::gOnly().bom());
        expected += L"<?xml version=\"1.0\" encoding=\"UTF-16\" ?>\n<resources>\n<phrase><id>101</id><string>String</string><notes>Notes</notes></phrase>\n<phrase><id>202</id><string>String 2</string><notes>Notes 2</notes></phrase>\n<phrase><id>303</id><string>String 3</string></phrase>\n</resources>\n";

        VERIFY (expected == xml.xml());
    }

}